// pages/user/requirement/requirement.js
import { CreateOrder } from '../../../api/order.js'
const moment = require('moment')

let _currentDateStr = moment().format('YYYY-MM-DD');
let _endDateStr = moment().add(1, 'days').format('YYYY-MM-DD');


Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: _currentDateStr,
    roomTypes: ['基本型', '大床房', '双床房'],
    region: ['上海市', '上海市', '宝山区'],
    startDate: _currentDateStr,
    endDate: _endDateStr,
    endDateStart: _endDateStr,
    dayCount: 1,
    hotelName: '',
    totalRoomCount: 0,
    singleRoomBudget: 500.00,
    totalPeopleCount: 2,
    kidCount: 0,
    roomInfos: [
    ],
    breakfast: false,
    noSmoking: false,
    hotelInvoice: false,
    housemate: true,
    arriveTime: '14:00',
    additionalBudget: 0.00,
    totalBudget: 0.00,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindRegion(e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindHotelName(e) {
    this.setData({
      hotelName: e.detail.value
    })
  },
  bindStartDate(e) {
    const currentDate = moment(_currentDateStr, 'YYYY-MM-DD');
    const startDate = moment(e.detail.value, 'YYYY-MM-DD');
    const endDate = startDate.add(1, 'days');

    if (startDate.isBefore(currentDate)) {
      wx.showToast({ title: '入住日期不能早于当前日期', icon: 'none', duration: 2000 });
      return;
    }

    this.setData({
      startDate: e.detail.value,
      endDate: endDate.format('YYYY-MM-DD'),
      endDateStart: endDate.format('YYYY-MM-DD'),
      dayCount: 1,
    });
  },
  bindEndDate(e) {
    const startDate = moment(this.data.startDate, 'YYYY-MM-DD');
    const endDate = moment(e.detail.value, 'YYYY-MM-DD');
    if (endDate.isBefore(startDate)) {
      wx.showToast({ title: '退房日期不能早于入住日期', icon: 'none', duration: 2000 });
      return;
    }

    this.setData({
      endDate: e.detail.value,
      dayCount: endDate.diff(startDate, 'days')
    })
  },
  addRoom(e) {
    this.data.roomInfos.push({
      roomType: 0,
      roomCount: 1
    });
    this.setData({
      roomInfos: this.data.roomInfos
    });
    this.calTotalRoomCount()
  },
  delRoom(e) {
    this.data.roomInfos.pop();
    this.setData({
      roomInfos: this.data.roomInfos
    });
    this.calTotalRoomCount()
  },
  bindRoomCount(e) {
    const index = e.currentTarget.dataset.index;
    const name = `roomInfos[${index}].roomCount`
    this.setData({
      [name]: Number(e.detail.value)
    });
    this.calTotalRoomCount()
  },
  bindRoomType(e) {
    const index = e.currentTarget.dataset.index;
    const name = `roomInfos[${index}].roomType`;
    this.setData({
      [name]: e.detail.value
    });
    console.log(this.data.roomInfos)
  },
  calTotalRoomCount() {
    let temp = 0;
    for (let i of this.data.roomInfos) {
      console.log(i);
      temp += i.roomCount
    }
    this.setData({
      totalRoomCount: temp
    });
    this.calTotalBudget();
  },
  bindSingleRoomBudget(e) {
    console.log(e)
    this.setData({
      singleRoomBudget: e.detail.value
    });
    console.log(this.data.singleRoomBudget)
    this.calTotalBudget();
  },
  bindTotalPeopleCount(e) {
    this.setDate({
      totalPeopleCount: e.detail.value
    });
  },
  bindKidCount(e) {
    this.setData({
      kidCount: e.detail.value
    });
  },
  bindBreakfast(e) {
    this.setData({
      breakfast: e.detail.value
    })
  },
  bindNoSmoking(e) {
    this.setData({
      noSmokeing: e.detail.value
    });
  },
  bindHotelInvoice(e) {
    this.setData({
      hotelInvoice: e.detail.value
    })
  },
  bindArriveTime(e) {
    this.setData({
      arriveTime: e.detail.value
    });
  },
  bindAdditionalBudget(e) {
    this.setData({
      additionalBudget: Number(e.detail.value)
    });
    this.calTotalBudget()
  },
  bindHousemate(e) {
    this.setData({
      housemate: e.detail.value
    })
  },
  calTotalBudget() {
    let total = this.data.singleRoomBudget * this.data.totalRoomCount + this.data.additionalBudget;
    console.log(total);
    this.setData({
      totalBudget: total
    });
  },
  createOrder(e) {
    if (this.data.hotelName === '') {
      wx.showToast({ title: '请输入酒店名称', icon: 'none', duration: 2000 });
      return;
    } else if (moment(this.data.startDate, 'YYYY-MM-DD').isBefore(moment(_currentDateStr))) {
      wx.showToast({ title: '入住日期不能早于当前日期', icon: 'none', duration: 2000 });
      return;
    } else if (moment(this.data.endDate, 'YYYY-MM-DD').isBefore(moment(this.data.startDate, 'YYYY-MM-DD'))) {
      wx.showToast({ title: '退房日期不能早于入住日期', icon: 'none', duration: 2000 });
      return;
    } else if (this.data.totalRoomCount == 0) {
      wx.showToast({ title: '请选择房间', icon: 'none', duration: 2000 });
      return;
    } else if (this.data.singleRoomBudget == 0) {
      wx.showToast({ title: '请输入单间预算', icon: 'none', duration: 2000 });
      return;
    } else if (this.data.totalPeopleCount == 0) {
      wx.showToast({ title: '请输入总人数', icon: 'none', duration: 2000 });
      return;
    }

    const params = {
      orderType: 'MULTI_ROOM',
      region: this.data.region,
      hotelName: this.data.hotelName,
      plannedCheckInDate: this.data.startDate,
      plannedCheckOutDate: this.data.endDate,
      dayCount: this.data.dayCount,
      singleRoomBudget: this.data.singleRoomBudget,
      roomInfos: this.data.roomInfos.map(input => {
        return {
          roomCount: input.roomCount,
          roomType: input.roomType == 0 && 'BaseType' ||
                    input.roomType == 1 && 'BigBed' ||
                    input.roomType == 2 && 'DoubleBed' ||
                    null
        }
      }),
      totalRoomCount: this.data.totalRoomCount,
      totalPeopleCount: this.data.totalPeopleCount,
      kidCount: this.data.kidCount,
      additionalBudget: this.data.additionalBudget,
      totalBudget: this.data.totalBudget,
      needBreakfast: this.data.breakfast,
      noSmoking: this.data.noSmokeing,
      needHotelInvoice: this.data.hotelInvoice,
      arriveTime: this.data.arriveTime,
      allowedHousemate: this.data.housemate
    };
    CreateOrder(params)
      .then(data => {
        wx.showToast({ title: '订单创建成功', icon: 'none', duration: 2000 });
        wx.navigateBack({});
      })
      .catch(err => {
        console.log(err);
        wx.showToast({ title: err.message, icon: 'none', duration: 2000 });
      })
  },
  toHome() {
    wx.navigateBack({
      delta: 2
    });
  }

});